package io.renren.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.config.DynamicDataSource;
import io.renren.vo.DataSourceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLException;

/**
 * @Author: Mr.Lee
 * @Date: 2022/8/13 12:13
 * @Description:
 *  数据源切换测试连接
 */
@CrossOrigin
@RestController
@RequestMapping("/datasource")
public class DynamicDataSourceController {
    @Autowired
    private DynamicDataSource dynamicDataSource;


    /**
     * 测试数据源的切换
     * @param dataSourceVo
     * @return
     * @throws SQLException
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    @PostMapping("/link")
    public String link(@RequestBody DataSourceVo dataSourceVo) throws SQLException, NoSuchFieldException, IllegalAccessException {
        //切换数据源之前先清空
        DynamicDataSource.clearDataSource();
        //切换数据源
        dynamicDataSource.createDataSource(dataSourceVo);

        System.out.println("当前数据源:"+dynamicDataSource.getConnection());

        return "当前数据源:"+dynamicDataSource.getConnection();
    }


}
